package controllers;

import java.sql.Timestamp;
import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import repository.SearchRepository;
import repository.UserRepository;
import validator.SearchFormValidator;

import command.MassiveSaveForm;
import command.SearchForm;

import domain.User;

@Controller
public class SearchController {

	private UserRepository userRepository;
	private SearchRepository searchRepository;
	private SearchFormValidator searchFormValidator; 
	private MessageSource msgSrc;
	
	@Autowired
	public SearchController(UserRepository userRepository,
			SearchRepository searchRepository,
			SearchFormValidator searchFormValidator,
			MessageSource msgSrc) {
		this.userRepository = userRepository;
		this.searchRepository = searchRepository;
		this.searchFormValidator = searchFormValidator;
		this.msgSrc = msgSrc;
	}
	
	@RequestMapping(method = RequestMethod.GET)
	protected ModelAndView Search(HttpSession session) {
		return SearchUtil(session, new SearchForm());
	}

	@RequestMapping(method = RequestMethod.GET)
	protected ModelAndView SearchUtil(HttpSession session, @RequestParam("search_id") SearchForm savedSearch) {
		ModelAndView mav = new ModelAndView();
		User user = (User) session.getAttribute("user");
		mav.addObject("savedSearches", searchRepository.getSavedSearchesByUser(user));
		mav.addObject("SearchForm", savedSearch);
		mav.addObject("action", "../search/SearchUtil");
		mav.setViewName("search/Search");
		return mav;
	}

	@RequestMapping(method = RequestMethod.POST)
	public ModelAndView SearchUtil(ModelAndView mav, HttpServletRequest request, HttpSession session, 
			SearchForm form, Errors errors) {
		User user = (User)session.getAttribute("user");
		if (request.getParameter("saveSearch") != null) {
			searchFormValidator.validate(form, errors);
			if (errors.hasErrors()) {
				mav.addObject("savedSearches", searchRepository.getSavedSearchesByUser(user));
				mav.addObject("SearchForm", form);
				mav.addObject("errorLogin", msgSrc.getMessage("errorAlGuardar", null,null));
				mav.addObject("action", "../search/SearchUtil");
				mav.setViewName("search/Search");
				return mav;
			}
			form.setUser(user);
			form.setDate(new Timestamp(Calendar.getInstance().getTime().getTime()));
			searchRepository.save(form);
			return SearchUtil(session, form);
		} else if (request.getParameter("choose") != null && !request.getParameter("choose").isEmpty()) {
			SearchForm savedSearch = searchRepository.get(Integer.parseInt(request.getParameter("choose")));
			return SearchUtil(session, savedSearch);
		} else {
			List<User> users = userRepository.search(form);
			String emails = "";
			for (int i=0; i<users.size(); i++) {
				emails += users.get(i).getEmail() + (i==users.size()-1 ? "" : ",");
			}
			mav.addObject("emails", emails);
			mav.addObject("users", users);
			mav.addObject("profiles", searchRepository.getSavedResultsByUser(user));
			mav.addObject("massiveSaveForm", new MassiveSaveForm());
			mav.setViewName("user/ListUsers");
			return mav;
		}
	}
	
	@RequestMapping(method = RequestMethod.GET)
	protected ModelAndView ListSavedSearches(HttpSession session) {
		User user = (User) session.getAttribute("user");
		List<SearchForm> searches = this.searchRepository.getSavedSearchesByUser(user);
		ModelAndView mav = new ModelAndView("search/ListSavedSearches");
		mav.addObject("searches", searches);
		return mav;
	}

	@RequestMapping(method = RequestMethod.GET)
	protected ModelAndView DeleteSavedSearch(HttpSession session, @RequestParam("search_id") SearchForm savedSearch) {
		this.searchRepository.delete(savedSearch);
		return ListSavedSearches(session);
	}
	
	
}
